#include "adLogic.h"
#include "adDao.h"
#include "glib.h"
#include "gio/gio.h"
#include <stdio.h>
#include "curlRequest.h"
#define RES_PATH "/home/fcl/ronghuanet_ads/resources/"


Ad* parseUpDownSplitScr(cJSON *adJson){
  Ad * result = calloc(1,sizeof(Ad));
  if (cJSON_IsObject(adJson))
  {
      //自己的一些属性解析
      cJSON *idJson = cJSON_GetObjectItemCaseSensitive(adJson,"id");
      int id = idJson->valueint;
      cJSON *templateIdJson = cJSON_GetObjectItemCaseSensitive(adJson,"template_id");
      int templateId = templateIdJson->valueint;
      cJSON *customerIdJson = cJSON_GetObjectItemCaseSensitive(adJson,"customer_id");
      int customerId = customerIdJson->valueint;
      cJSON *startTimeJson = cJSON_GetObjectItemCaseSensitive(adJson,"start_time");
      char *startTime = startTimeJson->valuestring;
      cJSON *endTimeJson = cJSON_GetObjectItemCaseSensitive(adJson,"end_time");
      char *endTime = endTimeJson->valuestring;
      result->id  = id;
      result->template_id  = templateId;
      result->customer_id  = customerId;
      result->start_time  = startTime;
      result->end_time  = endTime;
      //AdResource *resources[3];

      //广告资源解析
      AdResource *upAr = calloc(1,sizeof(AdResource));
      upAr->ad_id = id;
      upAr->index = 0;
      cJSON *typeIdJson = cJSON_GetObjectItemCaseSensitive(adJson,"top_media_type_id");
      int typeId = typeIdJson->valueint;
      upAr->type_id = typeId;
      cJSON *urlJson = cJSON_GetObjectItemCaseSensitive(adJson,"top_media_url");
      char *url = urlJson->valuestring;
      upAr->path = url;
      cJSON *signJson = cJSON_GetObjectItemCaseSensitive(adJson,"top_media_sign");
      char *sign = signJson->valuestring;
       upAr->sign = sign;
    
      result->adResources[0] =upAr;

       AdResource *buttomAr = calloc(1,sizeof(AdResource));
      buttomAr->ad_id = id;
      buttomAr->index = 0;
      cJSON *typeIdJson1 = cJSON_GetObjectItemCaseSensitive(adJson,"buttom_media_type_id");
      int typeId1 = typeIdJson1->valueint;
      buttomAr->type_id = typeId1;
      cJSON *urlJson1 = cJSON_GetObjectItemCaseSensitive(adJson,"buttom_media_url");
      char *url1 = urlJson1->valuestring;
      buttomAr->path = url1;
      cJSON *signJson1 = cJSON_GetObjectItemCaseSensitive(adJson,"buttom_media_sign");
      char *sign1 = signJson1->valuestring;
       buttomAr->sign = sign1;
    
      result->adResources[1] =buttomAr;
      result->adResources[2]=NULL;
  }
  return result;




}

char* getResPath(AdResource *ar){
  //获取ar对应的广告的id
  int adId= ar->ad_id;
  //获取文件的名字
  gchar* filename= g_path_get_basename(ar->path);
  //准备一个格式文件
  char *formatstr="%s/%d_%s";
  
  return g_strdup_printf(formatstr,RES_PATH,adId,filename);
  
}


int downloadAdsAndGenLocalAdListToSqlite(Ad* ads[],int size){
  int result=0;//返回数据的标识，判定是否有数据的更新/改变
  //1.遍历ads
  for (int i = 0; i < size; i++)
  {
    //2.获取ad的id
    Ad* ad= ads[i];
    int adId=ad->id;
    //3.去sqlite3 里面查询对应的Ad是否已经存在
    Ad* adInDb= getAd(adId);
    //4.如果不存在
    if (adInDb==NULL)
    {
      result=1;

      saveAd(ad);
      //处理对应的媒体文件
      for (int j = 0; j < 3; j++)
      {
        AdResource* ar= ad->adResources[j];
        if (ar!=NULL) //上下结构的 第三个是NULL
        {
          char* localArPath = getResPath(ar);
          //判断是否存在文件
          gboolean exits=  g_file_test(localArPath,G_FILE_TEST_EXISTS);
          if (!exits)
          {
            //创建文件
            GFile *file= g_file_new_for_path(localArPath);
            if (!g_file_create(file,0,NULL,NULL))
            {
              printf("create file error");
            }
            

          }
          //下载文件
          downloadRes(ar->path,localArPath);
          //保存对应的Ar
          ar->path=localArPath;
          saveAr(ar);

        }
      
      }
           
    }else{//当前的广告已经在数据库存在
      //更新一把
      updateAd(ad);

      for (int j = 0; j < 3; j++)
      {
        AdResource* ar= ad->adResources[j];
        if (ar!=NULL) //上下结构的 第三个是NULL
        {

          char* localArPath = getResPath(ar);
          char *sn = ar->sign;  //远程的加密串

          char *localSn="jfjjfjfjj";
          if(!g_str_equal(sn,localSn)){
             //判断是否存在文件
          gboolean exits=  g_file_test(localArPath,G_FILE_TEST_EXISTS);
          if (!exits)
          {
            result=1;  
            //创建文件
            GFile *file= g_file_new_for_path(localArPath);
            if (!g_file_create(file,0,NULL,NULL))
            {
              printf("create file error");
            }
            

          }
          //下载文件
          downloadRes(ar->path,localArPath);
          //保存对应的Ar
          ar->path=localArPath;
          saveAr(ar);
          }

        }
      
      }
           

    }
    
  }
  
  
  

  //5.如果存在
  /*
    5.1 更新Ad
    5.2 检查对应文件在本地是否存在，如果存在不用下载内容
  
  
  
  */

 //清理过期广告 --》建议使用定时任务




}


void handleAdList(cJSON* adListArray){
  //1.判断adListArray 是否是一个数组
  if (!cJSON_IsArray(adListArray))
  {  
    return;
  }
  //准备Ad数组 存放解析以后得数据
  int size= cJSON_GetArraySize(adListArray);
  Ad *ads[size];
  for (int i = 0; i < size; i++)
  {
    //获取第几个对象
    cJSON *adJsonStr=  cJSON_GetArrayItem(adListArray,i);
    //获取模板的类型
    int templateId = cJSON_GetObjectItemCaseSensitive(adJsonStr,"template_id")->valueint;
    switch (templateId)
    {
    case 6://上下分屏幕 广告结构
      Ad * ad = parseUpDownSplitScr(adJsonStr);
      ads[i]=ad;

      break;
    case 5:
      
      break;  
    
    default:
      g_print("template_id 是非法的.\n");
      break;
    }


  }
  
  //3.准备把数据存储到sqlite3，并且进行资源同步
  int result= downloadAdsAndGenLocalAdListToSqlite(ads,size);//返回值是1 有资源发生变化

  //4.把new资源释放


  //5.如果资源/广告列表有更新变化，qt进程重新加载内容
  if (result==1)
  {
    //进程之间的通讯
  }
  

  




}

  